package com.guli.mall.serviceBase.config;
/*
 * @Author 罗
 * @date 2020/9/10 - 9:29 下午
 */

import com.google.common.base.Predicates;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Data
@Slf4j
@EnableSwagger2
public class Swagger2Config {

    /**
     * 访问地址 /swagger-ui.html
     * 注意使用的swagger 2.9.2 ，swagger 3.0.0 不是这么配置
     * 使用了自动配置
     * 否则就要在调用此配置类maven坐标的启动类上添加 @ComponentScan(basePackages = "com.guli.serviceBase.config")
     */
    @Bean
    @ConditionalOnBean
    public Docket webApiConfig() {

        log.info("正在初始化swagger 2");

        // negate() 代表当接口中含有 /admin/.* 的时候就不进行显示
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .paths(Predicates.not(PathSelectors.regex("/actuator.*")))
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("谷粒商城Api文档")
                .description("本文档描述了商城中心微服务的定义")
                .version("1.0")
                .contact(new Contact("罗俊华", "http://www.bilibili.com", "1007052116@qq.com"))
                .build();
    }
}
